package com.atguigu.tingshu.search.service.impl;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.album.client.CategoryFeignClient;
import com.atguigu.tingshu.album.client.TrackInfoFeignClient;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.user.client.UserListenProcessFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {
    @Autowired
    private AlbumInfoFeignClient albumInfoFeignClient;
    @Autowired
    private TrackInfoFeignClient trackInfoFeignClient;
    @Autowired
    private CategoryFeignClient categoryFeignClient;
    @Autowired
    private UserInfoFeignClient userInfoFeignClient;
    @Autowired
    private UserListenProcessFeignClient userListenProcessFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;



    @Override
    public Map<String, Object> getItem(Long albumId) {
        //创建map集合
        Map<String,Object> result = new HashMap<>();
//        多线程+异步查询数据，使用completableFuture+Async：
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
//        远程调用查询专辑信息的微服务：
            Result<AlbumInfo> infoResult = albumInfoFeignClient.getAlbumInfo(albumId);
            Assert.notNull(infoResult, "查询专辑信息为空！");
            AlbumInfo albumInfo = infoResult.getData();
            Assert.notNull(albumInfo, "查询专辑信息失败！");
            result.put("albumInfo", albumInfo);
//            返回数据
            return albumInfo;

        },threadPoolExecutor).whenCompleteAsync((v, t) -> {
            System.out.println("专辑标题:"+v.getAlbumTitle());
            if (t != null){
                log.error("查询分类数据失败：{}",t.getMessage());
            }
        });
//        获取分类数据
        CompletableFuture<Void> cateCompleteFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
//            远程调用：
            Result<BaseCategoryView> baseCategoryViewResult = categoryFeignClient.getBaseCategoryView(albumInfo.getCategory3Id());
            Assert.notNull(baseCategoryViewResult, "查询分类数据为空！");
            BaseCategoryView baseCategoryView = baseCategoryViewResult.getData();
            Assert.notNull(baseCategoryView, "查询分类数据失败！");
//            存储数据
            result.put("baseCategoryView", baseCategoryView);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null){
                log.error("查询分类数据失败：{}",t.getMessage());
            }
        });
//        获取用户id
        CompletableFuture<Void> userCompleteFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            Result<UserInfoVo> userInfoVoResult = userInfoFeignClient.getUserInfoVo(albumInfo.getUserId());
            Assert.notNull(userInfoVoResult, "查询作者信息失败！");
            UserInfoVo userInfoVo = userInfoVoResult.getData();
            Assert.notNull(userInfoVo, "查询作者信息为空！");
            result.put("announcer", userInfoVo);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询作者数据失败：{}", t.getMessage());
            }
        });
//        根据专辑id查询专辑统计数据
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
//            远程调用
            Result<AlbumStatVo> albumStatVoResult = albumInfoFeignClient.getAlbumStatVo(albumId);
            Assert.notNull(albumStatVoResult, "查询专辑统计数据失败！");
            AlbumStatVo albumStatVo = albumStatVoResult.getData();
            Assert.notNull(albumStatVo, "查询专辑统计数据失败！");
            result.put("albumStatVo", albumStatVo);
        }, threadPoolExecutor).whenCompleteAsync((v, t) -> {
            if (t != null) {
                log.error("查询专辑统计信息失败：{}", t.getMessage());
            }
        });
        //      多任务组合
        CompletableFuture.allOf(albumInfoCompletableFuture,cateCompleteFuture,userCompleteFuture,statCompletableFuture).join();
        return result;
    }
}
